﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;

namespace UygulamaYetkiService
{
    public class ObjectVeXmlDAO : IDisposable
    {
        public XmlDocument CreateXML(Object classObject)
        {
            XmlDocument xmlDoc = null;
            try
            {
                xmlDoc = new XmlDocument();
                XmlSerializer xmlSerializer = new XmlSerializer(classObject.GetType());
                using (MemoryStream xmlStream = new MemoryStream())
                {
                    xmlSerializer.Serialize(xmlStream, classObject);
                    xmlStream.Position = 0;
                    xmlDoc.Load(xmlStream);
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
            }
            return xmlDoc;
        }

        public Object CreateObject(string XMLString, Object classObject)
        {
            try
            {
                XmlSerializer oXmlSerializer = new XmlSerializer(classObject.GetType());
                classObject = oXmlSerializer.Deserialize(new StringReader(XMLString));
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
            }
            return classObject;
        }

        public bool YetkilendirmeLogKaydet(string metotAdi, XmlDocument gelenXml, XmlDocument gidenXml, string gelenParametre, long kullaniciRef)
        {
            bool sonuc = false;
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TakbisBilgiSorguYetkiConnection"].ToString()))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    con.Open();
                    SqlTransaction transaction = con.BeginTransaction("trans");
                    try
                    {
                        cmd.CommandText = @"insert into YetkilendirmeLog with(updlock) (MetotAdi,GelenXml,GidenXml,KayitTarih,GelenParametre,KullaniciRef) values (@MetotAdi,@GelenXml,@GidenXml,getdate(),@GelenParametre,@Kullaniciref)";
                        cmd.Parameters.Add("@MetotAdi", metotAdi);

                        if (gelenXml != null)
                            cmd.Parameters.Add("@GelenXml", gelenXml.InnerXml);
                        else
                            cmd.Parameters.Add("@GelenXml", DBNull.Value);

                        if (gidenXml != null)
                            cmd.Parameters.Add("@GidenXml", gidenXml.InnerXml);
                        else
                            cmd.Parameters.Add("@GidenXml", DBNull.Value);

                        if (gelenParametre != null)
                            cmd.Parameters.Add("@GelenParametre", gelenParametre);
                        else
                            cmd.Parameters.Add("@GelenParametre", DBNull.Value);

                        cmd.Parameters.Add("@Kullaniciref", kullaniciRef);

                        cmd.Connection = con;
                        cmd.Transaction = transaction;
                        cmd.ExecuteScalar();
                        transaction.Commit();
                        sonuc = true;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        using (ExceptionLogDao el = new ExceptionLogDao())
                        {
                            el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                        }
                    }
                    finally
                    {
                        con.Close();
                        con.Dispose();
                        cmd.Dispose();
                    }
                    return sonuc;
                }
            }
        }

        public void Dispose()
        {
        }
    }
}

